package com.cf.invoker;

import com.cf.invoker.http.client.ClientServiceScanner;
import com.cf.invoker.http.server.JettyInvokServer;
import com.cf.invoker.http.server.RemoteServiceScanner;
import com.cf.invoker.http.server.ServiceRegisterStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Arrays;

/****
 *
 * @Author zhu-mingming
 * @Description
 * @Date 2018/10/23
 *
 **/

@Slf4j
@Configuration
@EnableConfigurationProperties(value = InvokerServerProperty.class)
public class HttpInvocationConfig {

    @Bean
    @ConditionalOnMissingBean(JettyInvokServer.class)
    public JettyInvokServer jettyInvokServer() {
        log.info("###创建JettyInvokServer####");
        return new JettyInvokServer();
    }

    @Bean
    @ConditionalOnMissingBean(ServiceRegisterStore.class)
    public ServiceRegisterStore serviceRegisterStore() {
        log.info("###创建ServiceRegisterStore####");
        return new ServiceRegisterStore();
    }

    @Bean
    @ConditionalOnMissingBean(RemoteServiceScanner.class)
    public RemoteServiceScanner remoteServiceScanner(@Autowired ServiceRegisterStore serviceRegisterStore) {
        log.info("###创建RemoteServiceScanner####");
        return new RemoteServiceScanner(serviceRegisterStore);
    }

    @Bean
    @ConditionalOnMissingBean(ClientServiceScanner.class)
    public ClientServiceScanner clientServiceScanner(@Autowired ClientHttpRequestFactory clientHttpRequestFactory) {
        log.info("###创建JClientServiceScanner####");
        return new ClientServiceScanner(restTemplate(clientHttpRequestFactory));
    }


    @Bean
    @ConditionalOnMissingBean(ClientHttpRequestFactory.class)
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //单位为ms
        factory.setReadTimeout(5000);
        //单位为ms
        factory.setConnectTimeout(5000);
        //消息转换器
        return factory;
    }


    private RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.setMessageConverters(Arrays.asList(new StringHttpMessageConverter(Charset.forName("utf-8")), new SourceHttpMessageConverter(), getApplicationJson(), getTextJson(), new AllEncompassingFormHttpMessageConverter()));
        return restTemplate;
    }


    /**
     * application/json  转换器
     *
     * @return
     */
    private MappingJackson2HttpMessageConverter getApplicationJson() {
        return new MappingJackson2HttpMessageConverter();
    }


    /**
     * text/json  转换器
     *
     * @return
     */
    private MappingJackson2HttpMessageConverter getTextJson() {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("text/json")));
        return mappingJackson2HttpMessageConverter;
    }

}
